package com.songlanyun.modules.marketing.groupbuy.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.utils.custom.DateCustomUtils;
import com.songlanyun.modules.account.entity.UserInfoEntity;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.marketing.groupbuy.model.vo.TeamMemberUserInfoVO;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyActivityGoodsService;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyActivityService;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyTeamMemberService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import lombok.Data;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 拼团活动的团
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-27 14:03:39
 */
@Data
@TableName("yxt_marketing_group_buy_team")
public class GroupBuyTeam extends RootEntity<Long, GroupBuyTeam> implements Serializable {
    private static final long serialVersionUID = 1L;

    {
        startTime = null;
        endTime = null;
        completeTime = null;
    }

    /**
     *
     */
    @TableId
    private Long id;
    /**
     * 所属商家
     */
    @NotNull(message = "所属商家不能为空")
    private Long shopId;
    /**
     * 拼团活动id
     */
    @NotNull(message = "拼团活动id不能为空")
    private Long groupBuyId;
    /**
     * 拼团活动商品id
     */
    @NotNull(message = "拼团活动商品id不能为空")
    private Long groupBuyGoodsId;

    /**
     * 团长会员id
     */
    @NotNull(message = "团长会员id不能为空")
    private Long leaderUserId;
    /**
     * 团长会员手机号
     */
    @NotBlank(message = "团长会员手机号不能为空")
    private String leaderUserMobile;
    /**
     * 成团人数
     */
    @NotNull(message = "成团人数不能为空")
    private Integer groupPersonNumber;
    /**
     * 成团小时数
     */
    @NotNull(message = "成团小时数不能为空")
    private Integer limitTime;

    /**
     * 建团时间
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;


    /**
     * 团长支付成功的开团时间
     */
    private Long startTime;

    /**
     * 团长支付成功的开团后的最终结束时间
     */
    public String getFormatStartTime() {
        return DateCustomUtils.formatToDateTimeString(startTime);
    }


    private Long endTime;

    /**
     * 获得倒计时的秒
     * @return
     */
    public Long getCountDownEndTime() {
        return this.endTime / 1000;
    }


    public String getFormatEndTime() {
        return DateCustomUtils.formatToDateTimeString(endTime);
    }


    /**
     * 成团时间（所有成员到位，所有订单可以开始发货）
     */
    private Date completeTime;
    /**
     * 0建团中（团长还没有支付），1建团成功，成团中（团长支付完成），2组团完成，3建团失败（团长未支付）4组团失败（时间到了人员不足）
     */
    private OrderConstant.TeamStatus status;

    @Override
    public Long getIdentity() {
        return getId();
    }


    @JsonIgnore
    @TableField(exist = false)
    private GroupBuyActivity groupBuyActivity;

    @JsonIgnore
    public GroupBuyActivity getGroupBuyActivity() {
        if (groupBuyActivity == null) {
            GroupBuyActivityService service = (GroupBuyActivityService) SpringContextUtil.getBean("groupBuyActivityService");
            groupBuyActivity = service.getById(this.getGroupBuyId(), true);
        }
        return groupBuyActivity;
    }


    @JsonIgnore
    @TableField(exist = false)
    private GroupBuyActivityGoods groupBuyActivityGoods;

    @JsonIgnore
    public GroupBuyActivityGoods getGroupBuyActivityGoods() {
        if (groupBuyActivityGoods == null) {
            GroupBuyActivityGoodsService service = (GroupBuyActivityGoodsService) SpringContextUtil.getBean("groupBuyActivityGoodsService");
            groupBuyActivityGoods = service.getById(getGroupBuyGoodsId());
        }
        return groupBuyActivityGoods;
    }

    /**
     * 获取团下面的所有成员
     */
    @TableField(exist = false)
    public List<GroupBuyTeamMember> allMember;

    @JsonIgnore
    public List<GroupBuyTeamMember> loadAllMember() {
        if (allMember == null) {
            GroupBuyTeamMemberService service = (GroupBuyTeamMemberService) SpringContextUtil.getBean("groupBuyTeamMemberService");
            allMember = service.loadAllMember(this.getId());
        }
        return allMember;
    }

    /**
     * 获取团下面所有有效支付的成员
     */

    @TableField(exist = false)
    public List<GroupBuyTeamMember> validMembers;

    @JsonIgnore
    public List<GroupBuyTeamMember> loadValidMembers() {
        if (validMembers == null) {
            GroupBuyTeamMemberService service = (GroupBuyTeamMemberService) SpringContextUtil.getBean("groupBuyTeamMemberService");
            validMembers = service.loadValidMember(this.getId());
        }
        return validMembers;
    }

    /**
     * 获取团下面所有参与成功的成员（参与成功，并不一定支付了，只是占了坑了，有过期支付参与失败的逻辑）
     */

    @TableField(exist = false)
    public List<GroupBuyTeamMember> joinSuccessMembers;

    public List<GroupBuyTeamMember> loadJoinSuccessMembers() {
        if (joinSuccessMembers == null) {
            GroupBuyTeamMemberService service = (GroupBuyTeamMemberService) SpringContextUtil.getBean("groupBuyTeamMemberService");
            joinSuccessMembers = service.loadJoinSuccessMember(this.getId());
        }
        return joinSuccessMembers;
    }

    @TableField(exist = false)
    public List<TeamMemberUserInfoVO> memberUserInfos;

    public List<TeamMemberUserInfoVO> loadMemberUserInfos() {
        if (memberUserInfos == null) {
            GroupBuyTeamMemberService service = (GroupBuyTeamMemberService) SpringContextUtil.getBean("groupBuyTeamMemberService");
            memberUserInfos = service.getTeamMembers(this.getId());
        }
        return memberUserInfos;
    }

    @JsonIgnore
    public Long computeEndTime() {
        return this.getStartTime() + this.getLimitTime() * 60 * 60 * 1000;
    }

    @TableField(exist = false)
    private GroupBuyTeamMember leaderMember;

    @JsonIgnore
    public GroupBuyTeamMember loadLeaderMember() {
        if (leaderMember == null) {
            GroupBuyTeamMemberService service = (GroupBuyTeamMemberService) SpringContextUtil.getBean("groupBuyTeamMemberService");
            leaderMember = service.getOne(new LambdaQueryWrapper<GroupBuyTeamMember>()
                    .eq(GroupBuyTeamMember::getBuyTeamId, this.getId())
                    .eq(GroupBuyTeamMember::getLeader, true)
                    .last("limit 1"));
        }
        return leaderMember;
    }

    @TableField(exist = false)
    private UserInfoEntity leaderUserInfo;

    public UserInfoEntity loadLeaderUserInfo() {
        if (leaderUserInfo == null) {
            UserInfoService service = (UserInfoService) SpringContextUtil.getBean("yxtUserInfoService");
            leaderUserInfo = service.getById(getLeaderUserId());
        }
        return leaderUserInfo;
    }

}
